﻿// ------------------------------------------------------------------------------
// <copyright file="PieItemsControl.cs" company="skumaronline.com">
//    Copyright (C) 2012 Suresh Kumar T.  All rights reserved.
// </copyright>
// ------------------------------------------------------------------------------

namespace StoreAppLib.Graph
{
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Markup;

    /// <summary>
    /// Pie internal class
    /// </summary>
    internal sealed class PieItemsControl : ItemsControl
    {
        /// <summary>
        /// Using a DependencyProperty as the backing store for tap effect for series.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty EnableTapEffectProperty =
            DependencyProperty.Register("EnableTapEffect", typeof(bool), typeof(PieItemsControl), new PropertyMetadata(true, PieItemsControl.OnTapEffectChanged));
  
        /// <summary>
        /// Initializes a new instance of the <see cref="PieItemsControl"/> class"
        /// </summary>
        public PieItemsControl()
        {
            this.ItemsPanel = (ItemsPanelTemplate)XamlReader.Load(@"<ItemsPanelTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ><Grid HorizontalAlignment='Stretch' VerticalAlignment='Stretch' /></ItemsPanelTemplate>");
            
            this.SetItemTemplate(false);
        }

        /// <summary>
        /// Gets or sets a value indicating whether tap effect to be applied for series.
        /// </summary>
        /// <value>
        /// The tap effect for series.
        /// </value>
        public bool EnableTapEffect
        {
            get { return (bool)this.GetValue(EnableTapEffectProperty); }
            set { this.SetValue(EnableTapEffectProperty, value); }
        }

        /// <summary>
        /// Fires when tap effect property changes
        /// </summary>
        /// <param name="source">The source</param>
        /// <param name="args">The argument</param>
        private static void OnTapEffectChanged(DependencyObject source, DependencyPropertyChangedEventArgs args)
        {
            if (args.OldValue != args.NewValue)
            {
                (source as PieItemsControl).SetItemTemplate((bool)args.NewValue);
            }
        }                

        /// <summary>
        /// Sets item template
        /// </summary>
        /// <param name="tapEffectEnabled">Whether tap effect enabled or not</param>
        private void SetItemTemplate(bool tapEffectEnabled)
        {
            this.ItemTemplate = (DataTemplate)XamlReader.Load(string.Format(@"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:graph='using:StoreAppLib.Graph' xmlns:animation='using:StoreAppLib.Animations'><graph:Arc animation:TapEffect.Enabled='{0}' TapCommand = '{{Binding TapCommand}}' StartAngle='{{Binding StartAngle}}' EndAngle='{{Binding EndAngle}}' Fill='{{Binding Fill}}'  Stroke='{{Binding Stroke}}' StrokeThickness='{{Binding StrokeThickness}}' /></DataTemplate>", tapEffectEnabled));
        }
    }
}
